package com.icare.common.service.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * bean对象类型拷贝转换
 *
 * @author TianHaibo
 * @create 2018-01-12 16:38
 **/
public class BeanMapper {

    public static <T>  T copy(Object src,Class<T> t) {
        try {
            if(src == null)return null;
            T newInstance = (T)Class.forName(t.getName()).newInstance();
            //拷贝属性
            Class<?> srcClass = src.getClass();
            Field[] declaredFields = srcClass.getDeclaredFields();
            for (Field f:declaredFields) {
                if(Modifier.isStatic(f.getModifiers()))//忽略static
                    continue;
                if(Modifier.isFinal(f.getModifiers()))//忽略final
                    continue;
                f.setAccessible(true);
                String key = f.getName();
                Field newF = t.getDeclaredField(key);
                if(newF == null)
                    continue;
                if(!f.getType().equals(newF.getType()))
                    continue;
                Object val = f.get(src);
                if(val == null) {
                    continue;
                }

                newF.setAccessible(true);
                newF.set(newInstance,val);
            }
            return newInstance;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T>  T copy(Object src,Class<T> t,String...ignores) {
        try {
            if(src == null)return null;
            List<String> ignoresF = Arrays.asList(ignores);
            T newInstance = (T)Class.forName(t.getName()).newInstance();
            //拷贝属性
            Field[] declaredFields = src.getClass().getDeclaredFields();
            for (Field f:declaredFields) {
                if(Modifier.isStatic(f.getModifiers()))
                    continue;
                if(Modifier.isFinal(f.getModifiers()))
                    continue;
                f.setAccessible(true);
                String key = f.getName();
                if(ignoresF.contains(key))//忽略指定属性
                    continue;
                Field newF = t.getDeclaredField(key);
                if(newF == null)
                    continue;
                if(!f.getType().equals(newF.getType()))
                    continue;
                Object val = f.get(src);
                if(val == null) {
                    continue;
                }
                newF.setAccessible(true);
                newF.set(newInstance,val);
            }
            return newInstance;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T>  T map(Object src,Class<T> t){
        if(src == null){
            return null;
        }
        String s = JSON.toJSONString(src);
        return JSON.parseObject(s,t);
    }

    public static <T>  List<T> mapList(List srcList,Class<T> t){
        if(srcList == null){
            return null;
        }
        String s = JSON.toJSONString(srcList);
        return JSON.parseArray(s,t);
    }
}
